package com.bluebench.bigul.domain.graphics.simple.polygon;

import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

import com.bluebench.bigul.domain.graphics.Graphic;
import com.bluebench.bigul.domain.graphics.Line;
import com.bluebench.bigul.domain.graphics.Point;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.pdf.PdfContentByte;

public abstract class Polygon extends Graphic{
	
	public static final int TRIANGLE=1; 
	public static final int SQUARE=2; 
	public static final int RECTANGLE=3; 
	public static final int TRAPEZIUM=4; 
	public static final int RHOMBUS=5; 
	public static final int PENTAGON=6; 
	public static final int HEXAGON=7; 
	public static final int HEPTAGON=8; 
	public static final int OCTAGON=9; 
	public static final int NANOGON=10;
	public static final int DECAGON=11; 
	
	
	
	protected List<Line> sides;
	
	public Polygon() {
	
	}
	
	public Polygon(PdfContentByte canvas) {
		super(canvas);
		sides = new ArrayList<Line>();
	}

	
	abstract Polygon build(Point start, List<Float> sides);
		
	/**
	 * Generate polygon using vertices instance variables
	 */
	@Override
	public Graphic build() {
		if(this.validate()){
			//canvas.setColorStroke(BaseColor.BLACK);
			Point start = vertices.get(0);
			canvas.moveTo(start.getX(), start.getY());
			for(int i=1;i<vertices.size();i++){
				Point p = vertices.get(i);
				float x = p.getX();
				float y = p.getY();
				canvas.lineTo(x,y);
			}
	        //canvas.closePathStroke();
		}
		return this;
	}


	public List<Line> getSides() {
		return sides;
	}


	public void setSides(List<Line> sides) {
		this.sides = sides;
	}	
	
	
	public void addSide(Line side){
		sides.add(side);
	}
	
	protected Point getRotation(Point start, Point pivot, float degrees){
		
		Point2D.Double result = new Point2D.Double();
	    Point2D.Double point = new Point2D.Double(start.getX(), start.getY());
		AffineTransform rotation = new AffineTransform();
	    double angle = (degrees * Math.PI / 180);
	    rotation.rotate(angle, pivot.getX(), pivot.getY());
	    rotation.transform(point, result);
	    
		return new Point((float)result.getX(),(float)result.getY());		
	}
	
	protected Point getPivot(Line... lines) {
		
		 List<Point> midpoints = new ArrayList<Point>();	
		 
		 for(Line l : lines){
			 Point p = l.getMidpoint();
			 midpoints.add(p);
		 }
		 
		 Line midline1 = new Line(midpoints.get(0),midpoints.get(2));
		 Line midline2 = new Line(midpoints.get(1),midpoints.get(3));
		 
		 Point p = midline1.getIntersection(midline2);
		 
		 return p;
		}
	
	
	
	
}
